package com.example.actionsignin.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.example.actionsignin.R;
import com.example.actionsignin.adapter.ActionAdapter;
import com.example.actionsignin.entity.ActionEntity;
import com.example.actionsignin.entity.ImagesEntity;
import com.example.actionsignin.entity.UserEntity;
import com.example.actionsignin.http.HttpUtil;
import com.example.actionsignin.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 旭 on 2017/7/27/027.
 */

public class ClassFragment extends Fragment implements View.OnClickListener {

    private View view;
    private ListView mListView;
    private List<ActionEntity<UserEntity,ImagesEntity>> mList = new ArrayList<>();
    private ActionAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_class,container,false);

        initView();

        return view;
    }


    private void initView() {
        mListView = (ListView) view.findViewById(R.id.list_class);

        getData();
        mAdapter = new ActionAdapter(getContext(),mList);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void getData()
    {
        HttpUtil.onSend().getAllAction()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ActionEntity<UserEntity,ImagesEntity>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<ActionEntity<UserEntity,ImagesEntity>> list) {
                        mList.clear();
                        for (int i = list.size()-1; i >= 0 ; i--) {
                            ActionEntity<UserEntity,ImagesEntity> action = list.get(i);
                            action.getType();

                            mList.add(action);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
}
